package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.CancelReasonDto;
import com.qf.commerce.common.vo.CancelReasonVo;
import com.qf.commerce.exception.ControllerException;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.resp.ResponseStatus;
import com.qf.commerce.service.CancelReasonService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cancel")
public class CancelReasonController {
    @Resource
    CancelReasonService cancelReasonService;
    @GetMapping("/list")
    public ResponseResult<PageInfo<CancelReasonVo>> queryCancelReason(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "20")int size){
        return ResponseResult.success(cancelReasonService.queryCancelReason(page, size));
    }

    @PutMapping("/update")
    public ResponseResult<Integer> upCancelById(@RequestBody CancelReasonDto cancelReasonDto){
        if(ObjectUtils.isEmpty(cancelReasonDto)){
            throw new ControllerException(ResponseStatus.GET_DATA_ERROR);
        }
        return ResponseResult.success(cancelReasonService.upById(cancelReasonDto));
    }

    @PostMapping("/add")
    public ResponseResult<Integer> addCancel(@RequestBody CancelReasonDto cancelReasonDto){
        if(ObjectUtils.isEmpty(cancelReasonDto)){
            throw new ControllerException(ResponseStatus.GET_DATA_ERROR);
        }
        return ResponseResult.success(cancelReasonService.addCancel(cancelReasonDto));
    }

    @DeleteMapping("/del")
    public ResponseResult<Integer> delCancelById(@RequestBody List<Long> cancelReasonId){
        if(ObjectUtils.isEmpty(cancelReasonId)){
            throw new ControllerException(ResponseStatus.GET_DATA_ERROR);
        }
        return ResponseResult.success(cancelReasonService.delById(cancelReasonId));
    }
}
